package com.wujilin.okra.activity;

import static android.view.KeyEvent.KEYCODE_MENU;
import static com.wujilin.okra.Okra.COLOR_BRIGHTEN;
import static com.wujilin.okra.Okra.COLOR_GREY600;
import static com.wujilin.okra.Okra.EXTRA_LOADER_ID;
import static com.wujilin.okra.Okra.LOADER_ARCHIVE_LIST;
import static com.wujilin.okra.Okra.LOADER_CATEGORY_LIST;
import static com.wujilin.okra.Okra.LOADER_OKRA_LIST;
import static com.wujilin.okra.Okra.REQUEST_CATEGORY_CHOOSE;
import static com.wujilin.okra.Okra.REQUEST_CATEGORY_EDIT;
import static com.wujilin.okra.Okra.REQUEST_CATEGORY_NEW;
import static com.wujilin.okra.Okra.REQUEST_OKRA;
import static com.wujilin.okra.Okra.REQUEST_OKR_DELETE;
import static com.wujilin.okra.Okra.REQUEST_OKR_SEARCH;
import static com.wujilin.okra.Okra.REQUEST_OKR_LIST;
import static com.wujilin.okra.Okra.TAG_ARCHIVE;
import static com.wujilin.okra.Okra.TAG_CATEGORY;
import static com.wujilin.okra.Okra.TAG_DIALOG_CATEGORY_NEW;
import static com.wujilin.okra.Okra.TAG_DIALOG_OBJECTIVE_NEW;
import static com.wujilin.okra.Okra.TAG_HOME;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;

import com.wujilin.okra.Okra;
import com.wujilin.okra.R;
import com.wujilin.okra.fragment.ArchiveListFragment;
import com.wujilin.okra.fragment.CategoryListFragment;
import com.wujilin.okra.fragment.CategoryNewFragment;
import com.wujilin.okra.fragment.CategoryEditFragment;
import com.wujilin.okra.fragment.ObjectiveNewFragment;
import com.wujilin.okra.fragment.OkraListFragment;

/**
 * @author Belin Wu
 */
public class MainActivity extends BaseActivity implements OnClickListener, 
/* onViewOkra */ ObjectiveNewFragment.Callback, OkraListFragment.Callback,
/* onViewOkraList */ CategoryListFragment.Callback, ArchiveListFragment.Callback,
					CategoryEditFragment.Callback {
    private View homeTab;
    private View categoryTab;
    private View archiveTab;

    private TextView homeText;
    private TextView categoryText;
    private TextView archiveText;
    
    private ImageView homeImage;
    private ImageView categoryImage;
    private ImageView archiveImage;
    
    private Menu menu;

    private static final String TAG = "MainActivity";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        doSelection(R.id.tab_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        this.menu = menu;
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	if (keyCode == KEYCODE_MENU) {
    		this.menu.performIdentifierAction(R.id.menu_overflow, 0);
    		return true;
    	}
    	return super.onKeyUp(keyCode, event);
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    		case R.id.menu_search:
    			Intent intent = new Intent(this, SearchActivity.class);
    			
    			startActivityForResult(intent, REQUEST_OKR_SEARCH);
    			break;
			case R.id.add_okra:
				ObjectiveNewFragment objectiveFragment = new ObjectiveNewFragment();
				objectiveFragment.show(getFragmentManager(), TAG_DIALOG_OBJECTIVE_NEW);
				break;
			case R.id.menu_add_group:
				CategoryNewFragment categoryFragment = new CategoryNewFragment();
				categoryFragment.show(getFragmentManager(), TAG_DIALOG_CATEGORY_NEW);
				categoryFragment.setTargetFragment(getFragment(TAG_CATEGORY), REQUEST_CATEGORY_NEW);
				break;
			case R.id.menu_exit:
				finish();
				break;
			default:
				return super.onOptionsItemSelected(item);
		}
    	return true;
    }
    
    private void initView() {
        homeTab = findViewById(R.id.tab_home);
        categoryTab = findViewById(R.id.tab_category);
        archiveTab = findViewById(R.id.tab_archive);

        homeText = (TextView) findViewById(R.id.text_home);
        categoryText = (TextView) findViewById(R.id.text_category);
        archiveText = (TextView) findViewById(R.id.text_archive);
        
        homeImage = (ImageView) findViewById(R.id.image_home);
        categoryImage = (ImageView) findViewById(R.id.image_category);
        archiveImage = (ImageView) findViewById(R.id.image_archive);

        homeTab.setOnClickListener(this);
        categoryTab.setOnClickListener(this);
        archiveTab.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_home: doSelection(R.id.tab_home); break;
            case R.id.tab_category: doSelection(R.id.tab_category); break;
            case R.id.tab_archive: doSelection(R.id.tab_archive); break;
        }
    }

    private void doSelection(int tabId) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        cleanSelection(transaction);
        Fragment listFragment = null;
        switch (tabId) {
            case R.id.tab_home:
                homeText.setTextColor(COLOR_BRIGHTEN);
                homeImage.setImageResource(R.drawable.ic_home_selected);
                listFragment = getFragment("home");
                if (listFragment == null) {
                	listFragment = new OkraListFragment();
                	Bundle args = new Bundle();
                	args.putInt(EXTRA_LOADER_ID, LOADER_OKRA_LIST);
                	listFragment.setArguments(args);
                    transaction.add(R.id.container_main, listFragment, "home");
                } else {
                	transaction.show(listFragment);
                }
                break;
            case R.id.tab_category:
                categoryText.setTextColor(COLOR_BRIGHTEN);
                categoryImage.setImageResource(R.drawable.ic_category_selected);
                listFragment = getFragment("category");
                if (listFragment == null) {
                	listFragment = new CategoryListFragment();
                    transaction.add(R.id.container_main, listFragment, "category");
                } else {
                	transaction.show(listFragment);                	
                }
                break;
            case R.id.tab_archive:
                archiveText.setTextColor(COLOR_BRIGHTEN);
                archiveImage.setImageResource(R.drawable.ic_archive_selected);
                listFragment = getFragment("archive");
                if (listFragment == null) {
                	listFragment = new ArchiveListFragment();
                    transaction.add(R.id.container_main, listFragment, "archive");
                } else {
                	transaction.show(listFragment);
                }
                break;
        }
        transaction.commit();
    }
    
    private void cleanSelection(FragmentTransaction transaction) {
        homeText.setTextColor(COLOR_GREY600);
        categoryText.setTextColor(COLOR_GREY600);
        archiveText.setTextColor(COLOR_GREY600);
        
        homeImage.setImageResource(R.drawable.ic_home_grey600);
        categoryImage.setImageResource(R.drawable.ic_category_grey600);
        archiveImage.setImageResource(R.drawable.ic_archive_grey600);
        
        hideFragment(transaction, getFragment("home"));
        hideFragment(transaction, getFragment("category"));
        hideFragment(transaction, getFragment("archive"));
    }
    
    private void hideFragment(FragmentTransaction transaction, Fragment fragment) {
    	if (fragment != null) {
    		transaction.hide(fragment);
    	}
    }

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case REQUEST_OKRA:
			case REQUEST_OKR_DELETE:
			case REQUEST_OKR_LIST:
			case REQUEST_OKR_SEARCH:
				restartLoader(LOADER_ARCHIVE_LIST, TAG_ARCHIVE);
			case REQUEST_CATEGORY_EDIT:
			case REQUEST_CATEGORY_CHOOSE:
				restartLoader(LOADER_OKRA_LIST, TAG_HOME);
				restartLoader(LOADER_CATEGORY_LIST, TAG_CATEGORY);
				break;
		}
	}
	
	@SuppressWarnings("unchecked")
	private void restartLoader(int loaderId, String fragmentTag) {
		LoaderCallbacks<Cursor> callback = (LoaderCallbacks<Cursor>) getFragmentManager().findFragmentByTag(fragmentTag);
		if (callback == null) {
			return;
		}
		getLoaderManager().restartLoader(loaderId, null, callback);
	}

	@Override
	public void onViewOkraList(Intent intent) {
		intent.setClass(this, OkraListActivity.class);
		startActivityForResult(intent, REQUEST_OKR_LIST);
	}

	@Override
	public void onOkraDeleted() {
		onActivityResult(REQUEST_OKR_DELETE, RESULT_OK, null);
	}
	
	@Override
	public void onOkraMoved() {
		onActivityResult(REQUEST_CATEGORY_CHOOSE, RESULT_OK, null);		
	}

	@Override
	public void onCategoryRenamed(String category) {
		onActivityResult(REQUEST_CATEGORY_EDIT, RESULT_OK, null);		
	}
}
